import { build, defineConfig, esbuildVersion } from 'vite'
import react from '@vitejs/plugin-react'
import vitecompression from 'vite-plugin-compression'
let time = Date.now()
// https://vite.dev/config/
// 自定义插件
const MyPlugins = () => {
  return {
    name: 'MyPlugins',
    apply: 'build',
    transform: (code: any, id: any) => {
      if (id.includes(('node_modules'))) {
        return code
      }
      if (/\.(ts|js|tsx|jsx)$/.test(id)) {
        return code.replaceAll('console.log()', '')
      }
    }
  }
}
export default defineConfig(({ mode }): any => {
  return {
    plugins: [
      react(),
      MyPlugins(),
      vitecompression({
        ext: 'gz',
        algorithm: 'gzip',
        threshold: 500 * 1024
      })
    ],
    // 打包
    build: {
      outDir: 'viteBuild',
      copyPublicDir: true,
      rollupOptions: {
        output: {
          assetFileNames: `[ext]/[name]-[hash]-${time}[.css]`,
          chunkFileNames: `js/[name]-[hash]-${time}.js`,
          entryFileNames: `js/[name]-[hash]-${time}.js`,
        }
      }
    },
  }
})
